<%@ tag language="java" pageEncoding="UTF-8" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<%@ attribute name="pageData" type="com.pet.shop.dto.PageDto" required="true" %>
<%@ attribute name="url" type="java.lang.String" required="true" %>    
<%@ attribute name="urlParam" type="java.lang.String" required="false" %>    

<c:set var="url" value="${pageContext.request.contextPath}${url}"/>  

<c:choose> 
  <c:when test="${urlParam != ''}">   
    <c:set var="url" value="${url}?${urlParam}&"/>  
  </c:when> 
  <c:otherwise>   
    <c:set var="url" value="${url}?"/>  
  </c:otherwise> 
</c:choose> 

<div class="pagination-warp">
  <ul class="pagination pull-right" style="margin:0 0 10px;">
  	<c:if test="${pageData.totalRecord > 1}">
  		<c:if test="${pageData.page != 1}">
	    	<li><a href="${url}">&laquo;</a></li>
	    </c:if>
    	<c:if test="${pageData.page <= 4}">
	    	<c:forEach var="x" begin="1" end="${pageData.page}" step="1"> 
			     <li <c:if test="${pageData.page eq x}"> class="active"</c:if>><a href="${url}pageNo=${x}">${x}</a></li>
			</c:forEach>
	    </c:if>
	    <c:if test="${pageData.page > 4}">
	    	<c:forEach var="x" begin="${pageData.page - 4}" end="${pageData.page}" step="1"> 
			     <li <c:if test="${pageData.page eq x}"> class="active"</c:if>><a href="${url}pageNo=${x}">${x}</a></li>
			</c:forEach>
	    </c:if>
	    <c:if test="${pageData.totalPage - pageData.page > 4}">
	    	<c:forEach var="x" begin="${pageData.page + 1}" end="${pageData.page + 4}" step="1"> 
			     <li><a href="${url}pageNo=${x}">${x}</a></li>
			</c:forEach>
	    </c:if>
	    <c:if test="${pageData.totalPage - pageData.page <= 4}">
	    	<c:forEach var="x" begin="${pageData.page + 1}" end="${pageData.totalPage}" step="1"> 
			     <li><a href="${url}pageNo=${x}">${x}</a></li>
			</c:forEach>
	    </c:if>
	    <c:if test="${pageData.page != pageData.totalPage}">
	    	<li><a href="${url}pageNo=${pageData.totalPage}">&raquo;</a></li>
	    </c:if>
	    <li><a class="goPage" url="${url}pageNo=" totalPage="${pageData.totalPage}">Go</a></li>
	    <li><input id="pageIndex" type="text" class="form-control" value="${pageData.page}" placeholder="页码" style="display:inline;width:58px;"></li>
    </c:if>
  </ul>
 	<div class="pagination-tip">显示
 		<strong>${pageData.totalRecord == 0 ? 0 : (pageData.page - 1) * pageData.rows + 1} </strong>到
 		<strong>${pageData.page * pageData.rows < pageData.totalRecord ? pageData.page * pageData.rows : pageData.totalRecord}</strong>，共
 		<strong>${pageData.totalRecord}</strong> 条记录
 	</div>
</div>
<script>
$(function(){
	$(".goPage").click(function(){
		var url = $(this).attr("url");
		var totalPage = $(this).attr("totalPage");
		var pageNo = $("#pageIndex").val();
		if (pageNo == "") {
			$.messager.alert("请输入页码");
			$("#pageIndex").focus();
			return false;
		}
		if (pageNo != "" && !/^\d+$/.test(pageNo)) {
			$.messager.alert("页码只能为数字");
			$("#pageIndex").focus();
			return false;
		}
		console.log(pageNo>totalPage);
		if(pageNo > parseInt(totalPage)){
			pageNo = totalPage;
		}
		location.href = url + pageNo;
	});
});
</script>